home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / devs / new8n1 / 8n1_060.s < prev    next >
Text File  |  1980-12-01  |  61KB  |  2,053 lines

  1. ********************************************************************************
  2. **                                          **
  3. **  Name       : 8n1_060.s                              **
  4. **  Copyright  : © Copyright 96-97                          **
  5. **  Author     : Iain Barclay                              **
  6. **  Created    : 15 Jul 97                              **
  7. **  Version    : 43.3                                  **
  8. **                                          **
  9. ********************************************************************************
  10.      ;
  11.      ;      SNMA specific options
  12.      ;
  13.      IFD      SNMA
  14.      ;
  15.      CPU      M68060
  16.      ;
  17.      SNMAOPT  Q,A,M,T,E-,P,R,B
  18.      ;
  19.      ELSE
  20.      ; I have phxopts define this symbol
  21.      IFD PHXASS
  22.      machine 68060
  23.      FPU
  24.      ENDC
  25.      ENDC
  26.      ;
  27.      ;
  28. NEWCODE  SET    1
  29. SETDSR     SET    0
  30. NCOMM     SET    0
  31. FASTVBR  SET    1
  32. DANGER     SET    0
  33. NEGCHECK SET    1
  34. EOFCODE  SET    1
  35.      ;
  36.      ;
  37.      ;
  38.      SECTION  text,CODE
  39.      ;
  40.      ;
  41.      ;
  42. DEVICES_SERIAL_I_OBSOLETE  EQU        1
  43.      ;
  44.      ;
  45.      ;
  46.      INCLUDE  "exec/lists.i"
  47.      INCLUDE  "exec/memory.i"
  48.      INCLUDE  "exec/resident.i"
  49.      INCLUDE  "exec/devices.i"
  50.      INCLUDE  "exec/execbase.i"
  51.      INCLUDE  "exec/io.i"
  52.      INCLUDE  "exec/ports.i"
  53.      INCLUDE  "exec/errors.i"
  54.      INCLUDE  "exec/initializers.i"
  55.      INCLUDE  "intuition/preferences.i"
  56.      INCLUDE  "devices/timer.i"
  57.      INCLUDE  "devices/serial.i"
  58.      INCLUDE  "hardware/custom.i"
  59.      INCLUDE  "hardware/cia.i"
  60.      INCLUDE  "hardware/intbits.i"
  61.      INCLUDE  "hardware/adkbits.i"
  62.      INCLUDE  "resources/misc.i"
  63.      INCLUDE  "exec/alerts.i"
  64.      INCLUDE  "exec/macros.i"
  65.      INCLUDE  "8n1.device_rev.i"
  66.      ;
  67.      ;      Define hardware references
  68.      ;
  69.      XREF      _custom
  70.      XREF      _intena,_intenar,_intreq,_intreqr
  71.      XREF      _ciab,_ciabpra
  72.      XREF      _serper,_serdat,_serdatr
  73.      XREF      _adkcon,_adkconr
  74.      ;
  75.      ;      Exec Functions
  76.      ;
  77.      XREF      _LVORemDevice,_LVOOpenDevice,_LVOCloseDevice
  78.      XREF      _LVOSupervisor
  79.      XREF      _LVOAllocMem,_LVOFreeMem,_LVOCopyMem,_LVOTypeOfMem
  80.      XREF      _LVOReplyMsg
  81.      XREF      _LVOSendIO,_LVOAbortIO
  82.      XREF      _LVODisable,_LVOEnable
  83.      XREF      _LVOFindName
  84.      XREF      _LVOOpenResource
  85.      XREF      _LVOAddIntServer,_LVORemIntServer
  86.      XREF      _LVOAlert
  87.      XREF      _LVOOpenLibrary,_LVOCloseLibrary
  88.      XREF      _LVOCacheClearU
  89.      ;
  90.      ;      Misc Resource Functions
  91.      ;
  92.      XREF      _LVOAllocMiscResource,_LVOFreeMiscResource
  93.      ;
  94.      ;      Intuition Functions
  95.      ;
  96.      XREF      _LVOGetPrefs
  97.      ;
  98.      ;      Mask used to get rid of the printer bits.
  99.      ;
  100. PRTMASK  EQU      (CIAF_PRTRPOUT|CIAF_PRTRBUSY)
  101.      ;
  102.      ;      Autovector offsets
  103.      ;
  104. LVL1VEC  EQU      (1-1)*4+$64
  105. LVL5VEC  EQU      (5-1)*4+$64
  106. VBR_SIZE EQU      $400
  107. custom     EQU      $dff000
  108. ciab     EQU      $bfd000
  109.      ;
  110.      ;  New Style device stuff (not the best way but it will have too do for now)
  111.      ;  If u don't have New Style stuff defined elsewhere remove these ;
  112. ;NSCMD_DEVICEQUERY   EQU $4000
  113. ; STRUCTURE  NSDEVICEQUERYRESULT,0
  114. ;    ULONG   DEVQUERYFORMAT
  115. ;    ULONG   SIZEAVAILABLE
  116. ;    UWORD   DEVICETYPE
  117. ;    UWORD   DEVICESUBTYPE
  118. ;    APTR    SUPPORTEDCOMMANDS
  119. ;    LABEL   NSDEVICEQUERYRESULT_SIZE
  120. ;NSDEVTYPE_SERIAL     EQU 11
  121.      ;
  122.      ;      Device base
  123.      ;      pad to longword
  124.      ;
  125.      STRUCTURE Base8n1,LIB_SIZE
  126.      UBYTE      vb_pad1
  127.      UBYTE      vb_pad2
  128.      ULONG      vb_SaveDDRA
  129.      ULONG      vb_SavePRA
  130.      APTR      vb_MiscBase
  131.      APTR      vb_OldLevel1
  132.      APTR      vb_OldLevel5
  133.      ULONG      vb_SegList
  134.      ULONG      vb_DefBaud
  135.      ULONG      vb_DefRBufLen
  136.      ULONG      vb_CurRBuf
  137.      ULONG      vb_CurRBufLen
  138.      ULONG      vb_CurBaud
  139.      ULONG      vb_SerFlags
  140.      ULONG      vb_Initialized
  141.      LABEL      sizeof_Base8n1
  142.      ;
  143.      ;
  144.      ;
  145.      ;
  146.      ;
  147.      ;
  148. Start:
  149.      moveq      #-1,d0                  ; set return code
  150.      rts                          ; return
  151.      ;
  152.      ;      RamLib looks for this romtag
  153.      ;
  154. ROMTag     DC.W      RTC_MATCHWORD               ; RT_MATCHWORD
  155.      DC.L      ROMTag                  ; RT_MATCHTAG
  156.      DC.L      ENDTag                  ; RT_ENDSKIP
  157.      DC.B      RTF_AUTOINIT                  ; RT_FLAGS
  158.      DC.B      VERSION                  ; RT_VERSION
  159.      DC.B      NT_DEVICE                  ; RT_TYPE
  160.      DC.B      0                      ; RT_PRI
  161.      DC.L      Name                      ; RT_NAME
  162.      DC.L      IdString                  ; RT_IDSTRING
  163.      DC.L      Init                      ; RT_INIT
  164.      ;
  165.      ;      Perform device initialization
  166.      ;
  167.     cnop 0,4    ; align for 020+
  168. InitRoutine:
  169.      exg      d0,a0                   ; swap seglist and base
  170.      move.l   d0,vb_SegList(a0)              ; store seglist in base
  171.      move.l   a6,SysBase                  ; store in global storage
  172.      exg      a0,d0                   ; swap them back
  173.      rts                          ; return
  174.      ;
  175.      ;
  176.      ;
  177.     cnop 0,4 ; align for 020+
  178. dev_Open:   ; (a1 iorequest, a5 will be Base8n1, a6 sysbase )
  179.      addq.w   #1,LIB_OPENCNT(a6)              ; fake open so device is not expunged
  180.      move.l   a5,-(sp)                  ; save registers
  181.      move.l   a6,a5                   ; save base
  182.      ;
  183.      cmpi.w   #IOEXTSER_SIZE,MN_LENGTH(a1)          ; IORequest size >= IOEXTSER_SIZE
  184.      bcs      50$                      ; nope, finish
  185.      ;
  186.      tst.l      d0                      ; unit 0 specified?
  187.      bne      50$                      ; nope, error (who cares?)
  188.      ;
  189.      cmpi.w   #1,LIB_OPENCNT(a5)              ; currently open?
  190.      bne      10$                      ; yep, go process
  191.      ; IOExtSer
  192.      move.l   IO_READLEN(a1),vb_SerFlags(a5)     ; save flags
  193.      ;
  194.      tst.l      vb_Initialized(a5)              ; already initialized?
  195.      bne      40$                      ; yep, skip initialization
  196.      ;
  197.      movea.l  SysBase(pc),a6              ; get ExecBase
  198.      move.l   a1,-(sp)                  ; save register
  199.      ;
  200.      lea      miscresource(pc),a1              ; ptr to resource name
  201.      jsr      _LVOOpenResource(a6)              ; go open it
  202.      move.l   d0,vb_MiscBase(a5)              ; save base
  203.      ;
  204.      moveq      #MR_SERIALPORT,d0              ; set unit number
  205.      bsr      allocResource               ; go allocate it
  206.      tst.l      d0                      ; did we get it?
  207.      bne.b      20$                      ; nope, error
  208.      ;
  209.      moveq      #MR_SERIALBITS,d0              ; set unit number
  210.      bsr      allocResource               ; go allocate it
  211.      tst.l      d0                      ; did we get it?
  212.      bne.b      5$                      ; nope, error
  213.      ;
  214.      ;      Get system preferences
  215.      ;
  216.      lea      intuitlib(pc),a1              ; ptr to library name
  217.      moveq      #0,d0                   ; can't use OldOpenLibrary as this will not be supported.
  218.      jsr      _LVOOpenLibrary(a6)              ; go open it (any version)
  219.      movea.l  d0,a6                   ; get intuition base
  220.      ;
  221.      move.l   #(pf_SerParShk+3)&$fffffffc,d0      ; size we need (aligned)
  222.      suba.l   d0,sp                   ; reserve space
  223.      ;
  224.      movea.l  sp,a0                   ; set data area ptr
  225.      jsr      _LVOGetPrefs(a6)              ; get preferences
  226.      ;
  227.      movea.l  a6,a1                   ; get intuition base
  228.      movea.l  SysBase(pc),a6              ; restore ExecBase
  229.      jsr      _LVOCloseLibrary(a6)              ; close it
  230.      ;
  231.      moveq      #$0f,d0                  ; set mask
  232.      and.b      pf_SerStopBuf(sp),d0              ; get bufsize index
  233.      moveq      #0,d1                   ; clear upper half
  234.      addq.l   #8,d0                   ; calc shift value
  235.      move.w   pf_BaudRate(sp),d1              ; get baud rate
  236.      lsl.l      #2,d0                   ; get default bufsize
  237.      move.l   d0,vb_DefRBufLen(a5)              ; and store
  238.     IFNE NEWCODE
  239.      move.l   .baudTable(pc,d1.l*4),d1          ; get baud rate
  240.     ELSE
  241.      move.w   .baudTable(pc,d1.l*2),d1          ; get baud rate
  242.     ENDC
  243.      ;
  244.      move.l   d1,vb_DefBaud(a5)              ; and store
  245.      ;
  246.      bsr      internalReset               ; go init baud and buffer
  247.      ;
  248.      lea      (pf_SerParShk+3)&$fffffffc(sp),sp   ; restore stack
  249.      tst.l      d0                      ; got 'em?
  250.      beq.b      30$                      ; yep, branch
  251.      ;
  252.      moveq      #MR_SERIALBITS,d0              ; set unit
  253.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  254.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  255.      ;
  256. 5$     moveq      #MR_SERIALPORT,d0              ; set unit
  257.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  258.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  259.      ;
  260. 20$     moveq      #SerErr_DevBusy,d0              ; set error status
  261.      move.l   (sp)+,a1                  ; restore registers
  262.      bra      50$                      ; go return
  263.      ;
  264.      ;      Preferences baud lookup table
  265.      ;
  266.      cnop 0,4 ; align for 020+
  267.     IFNE NEWCODE
  268. .baudTable:
  269.      dc.l      112,300,1200,2400,4800,9600,19200,31250,38400,57600,62400,64800
  270.      dc.l      76800,115200
  271.     ELSE
  272. .baudTable:
  273.      dc.w      112,300,1200,2400,4800,9600,19200,31250,38400,57600,62400,64800
  274.     ENDC
  275.      ;
  276.      cnop 0,4 ; align for 020+
  277. 30$     moveq      #0,d1                   ; clear flags
  278.      lea      timerReq(pc),a1              ; ptr to timer request
  279.      moveq      #UNIT_VBLANK,d0              ; set unit
  280.      lea      timerdevice(pc),a0              ; ptr to device name
  281.      jsr      _LVOOpenDevice(a6)              ; go open it
  282.      ;
  283.     IFNE DANGER
  284.      move.w   #$04000,_intena
  285.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  286.     ELSE
  287.      jsr      _LVODisable(a6)              ; disable interrupts
  288.     ENDC
  289.      ;
  290.      move.l   #ciab,a1                  ; get ptr to ciab
  291.      move.b   ciaddra(a1),d0
  292.      move.l   d0,vb_SaveDDRA(a5)              ; save DDR value
  293.      andi.b   #PRTMASK,d0                  ; make serial bits input
  294.      move.b   (a1),d1                  ; ciapra(a1)
  295.      ori.b      #CIAF_COMDTR|CIAF_COMRTS,d0          ; make DTR/RTS output
  296.      move.l   d1,vb_SavePRA(a5)              ; save PR value
  297.      andi.b   #CIAF_COMDTR|CIAF_COMRTS|PRTMASK,d0 ; turn on DTR/RTS
  298.      move.b   d0,ciaddra(a1)
  299.      andi.b   #CIAF_COMCTS|CIAF_COMDSR|PRTMASK,d1 ; make CTS/DSR input
  300.      move.b   d1,(a1)                  ; ciapra(a1)
  301.      ;
  302.      moveq      #INTB_PORTS,d0              ; get interrupt number
  303.      lea      VBInterrupt(pc),a1              ; get interrupt ptr
  304.      jsr      _LVOAddIntServer(a6)              ; add it to the list
  305.      ;
  306.      bsr      getVBR                  ; get vector base (in A0)
  307.      ;
  308.     IFNE FASTVBR
  309.      movem.l  a0/a5,-(sp)                  ; save a5, VBR
  310.      move.l   a0,a1
  311.      jsr      _LVOTypeOfMem(a6)
  312.      btst      #MEMB_FAST,d0               ; VBR in fast?
  313.      bne.b      nofast$                  ; yep, branch
  314.      move.l   #VBR_SIZE,d0
  315.      moveq      #MEMF_PUBLIC|MEMF_FAST,d1
  316.      jsr      _LVOAllocMem(a6)              ; Alloc fast-ram for VBR
  317.      tst.l      d0
  318.      beq.b      nofast$                  ; no fast, branch
  319.      move.l   (sp),a0                  ; get VBR
  320.      move.l   d0,a1
  321.      move.l   d0,-(sp)                  ; save new VBR
  322.      move.l   #VBR_SIZE,d0
  323.      jsr      _LVOCopyMem(a6)
  324.      move.l   (sp)+,a0                  ; get new VBR
  325.      lea      setVBR$(pc),a5
  326.      jsr      _LVOSupervisor(a6)              ; set new VBR
  327.      jsr      _LVOCacheClearU(a6)              ; clear cache
  328.      move.l   (sp)+,d0                  ; was old VBR zero?
  329.      beq.b      waszero$                  ; yep, branch (don't free)
  330.      move.l   d0,a1
  331.      move.l   #VBR_SIZE,d0
  332.      jsr      _LVOFreeMem(a6)
  333. waszero$
  334.      bsr      getVBR
  335.      bra.b      vbend$
  336.      cnop      0,4
  337. setVBR$
  338.      movec      a0,vbr                  ; set VBR
  339.      rte
  340.      cnop      0,4
  341. nofast$
  342.      move.l   (sp)+,a0                  ; restore VBR
  343. vbend$
  344.      move.l   (sp)+,a5                  ; restore a5
  345.     ENDC
  346.      move.l   LVL1VEC(a0),vb_OldLevel1(a5)          ; save original vector
  347.      lea      level1(pc),a1               ; get new vector ptr
  348.      move.l   a1,LVL1VEC(a0)              ; set new vector
  349.      ;
  350.      move.l   LVL5VEC(a0),vb_OldLevel5(a5)          ; save original vector
  351.      lea      level5(pc),a1               ; get new vector ptr
  352.      move.l   a1,LVL5VEC(a0)              ; set new vector
  353.      ;
  354.      move.l   #custom+intena,a1              ; get ptr to custom chips
  355.      move.w   #INTF_RBF|INTF_TBE,intreq-intena(a1); clear pending interrupts
  356.      move.w   #INTF_SETCLR|INTF_RBF|INTF_TBE,(a1) ; enable RBF & TBE
  357.      ;
  358.      addq.l   #1,vb_Initialized(a5)           ; set flag
  359.      ;
  360.     IFNE DANGER
  361.      subq.b   #1,IDNestCnt(a6)
  362.      bge.b      enable$
  363.      move.w   #$0C000,(a1)
  364. enable$
  365.     ELSE
  366.      jsr      _LVOEnable(a6)              ; enable interrupts
  367.     ENDC
  368.      ;
  369.      move.l   (sp)+,a1                  ; restore register
  370.      ;
  371.      bra.b      40$                      ; go exit
  372.      ;
  373.      cnop 0,4 ; align for 020+
  374. 10$     moveq      #SerErr_DevBusy,d0              ; preset error status
  375.      btst      #SERB_SHARED,vb_SerFlags+3(a5)      ; opened shared?
  376.      beq.b      50$                      ; nope, error
  377.      btst      #SERB_SHARED,IO_SERFLAGS(a1)          ; requesting shared?
  378.      beq.b      50$                      ; nope, error
  379.      ;
  380.      ;      Initialize I/O request
  381.      ;
  382. 40$
  383.      moveq      #8,d0                   ; get char size
  384.      move.b   d0,IO_READLEN(a1)              ; set read length
  385.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  386.      moveq      #1,d0                   ; get stop bits
  387.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  388.      move.b   IO_SERFLAGS(a1),d0
  389.      ori.b      #SERF_XDISABLED|SERF_RAD_BOOGIE|SERF_QUEUEDBRK|SERF_7WIRE,d0 ;flags
  390.      andi.b   #~(SERF_PARTY_ODD|SERF_PARTY_ON),d0 ; not used
  391.      move.b   d0,IO_SERFLAGS(a1)
  392.      move.l   vb_CurBaud(a5),IO_BAUD(a1)          ; set baud
  393.      moveq      #0,d0                   ; no error
  394.      move.l   vb_CurRBufLen(a5),IO_RBUFLEN(a1)    ; set read buffer length
  395.      ;
  396.      addq.w   #1,LIB_OPENCNT(a5)              ; incr open count
  397.      andi.b   #~(1<<LIBB_DELEXP),LIB_FLAGS(a5)    ; clear expunge bit
  398.      ;
  399. 50$     move.b   d0,IO_ERROR(a1)              ; store error code
  400.      movea.l  a5,a6                   ; restore base
  401.      move.l   (sp)+,a5                  ; restore registers
  402.      subq.w   #1,LIB_OPENCNT(a6)              ; remove fake open
  403.      rts                          ; return
  404.      ;
  405.      ;      Attempt to allocate one of the serial resources.
  406.      ;
  407.      cnop 0,4 ; align for 020+
  408. allocResource:
  409.      movem.l  d0/a6,-(sp)
  410.      lea      Name(pc),a1                  ; get lock name
  411.      move.l   vb_MiscBase(a5),a6              ; get MiscBase
  412.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  413.      tst.l      d0                      ; did we get it?
  414.      beq.b      20$                      ; yep, branch
  415.      ;
  416.      ;      It's in use, so we try to locate the device using the string
  417.      ;      returned and attempt to remove it.
  418.      ;
  419.      movea.l  SysBase(pc),a6              ; get ExecBase
  420.      ;
  421.      movea.l  d0,a1                   ; get ptr to serial name
  422.      lea      DeviceList(a6),a0              ; get ptr to device list
  423.      jsr      _LVOFindName(a6)              ; go find it
  424.      tst.l      d0                      ; found?
  425.      beq.b      10$                      ; nope, branch
  426.      ;
  427.      movea.l  d0,a1                   ; xfer device ptr
  428.      jsr      _LVORemDevice(a6)              ; remove it
  429.      ;
  430.      ;      We then retry the allocate.
  431.      ;
  432. 10$     move.l   (sp),d0                  ; get unit
  433.      lea      Name(pc),a1                  ; get lock name
  434.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  435.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  436.      ;
  437. 20$     addq.l   #4,sp                   ; restore stack ptr
  438.      move.l   (sp)+,a6                  ; restore base ptr
  439.      rts                          ; return
  440.      cnop 0,4 ; align for 020+
  441.      ;
  442.      ;      Set exception vectors
  443.      ;
  444.      cnop 0,4 ; align for 020+
  445. getVBR:
  446.      move.l   a5,-(sp)                  ; save registers
  447.      lea      10$(pc),a5                  ; ptr to routine
  448.      jsr      _LVOSupervisor(a6)              ; get into supervisor state
  449.      move.l   (sp)+,a5                  ; restore register
  450.      rts                          ; return
  451.      cnop      0,4
  452. 10$     movec.l  vbr,a0                  ; get vector base
  453.      rte                          ; return
  454.      ;
  455.      ;      Device Close routine
  456.      ;
  457.      cnop 0,4 ; align for 020+
  458. dev_Close:
  459.      ;
  460.      moveq      #-1,d0                  ; invalidate
  461.      move.l   d0,IO_DEVICE(a1)              ;   device
  462.      ;
  463.      tst.w      LIB_OPENCNT(a6)              ; check we are open
  464.      beq.b      5$                      ; nope, branch
  465.      subq.w   #1,LIB_OPENCNT(a6)              ; decr open count
  466.      bne      dev_Null                  ; still open? yep, branch
  467. 5$
  468.      ;
  469.      movem.l  a1/a5,-(sp)                  ; save registers
  470.      ;
  471.      movea.l  a6,a5                   ; save base
  472.      movea.l  SysBase(pc),a6              ; get ExecBase
  473.      ;
  474.      bsr.b      getVBR                  ; get vector base (in A0)
  475.      ;
  476.      moveq      #1,d0                   ; set not restored code
  477.      ;
  478.      lea      level1(pc),a1               ; get our vector ptr
  479.      cmpa.l   LVL1VEC(a0),a1              ; do they match?
  480.      bne      10$                      ; nope, can't restore
  481.      ;
  482.      lea      level5(pc),a1               ; get our vector ptr
  483.      cmpa.l   LVL5VEC(a0),a1              ; do they match?
  484.      bne      10$                      ; nope, can't restore
  485.      ;
  486.     IFNE DANGER
  487.      move.w   #$04000,_intena
  488.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  489.     ELSE
  490.      jsr      _LVODisable(a6)              ; disable interrupts
  491.     ENDC
  492.      ;
  493.      move.l   vb_OldLevel1(a5),LVL1VEC(a0)          ; restore original vector
  494.      move.l   vb_OldLevel5(a5),LVL5VEC(a0)          ; restore original vector
  495.      ;
  496.      moveq      #INTB_PORTS,d0              ; get interrupt number
  497.      lea      VBInterrupt(pc),a1              ; get interrupt ptr
  498.      jsr      _LVORemIntServer(a6)              ; remove it from the list
  499.      ;
  500.      move.l   #custom+intena,a0              ; get custom base
  501.      move.w   #INTF_RBF|INTF_TBE,d0           ; indicate serial interrupts
  502.      move.w   d0,(a0)                  ; disable interrupts
  503.      move.w   d0,intreq-intena(a0)              ; clear pending interrupts
  504.      ;
  505.      bsr      freeBuf                  ; free allocated buffers
  506.      ;
  507.      move.l   #ciab,a0                  ; get pointer to ciab
  508.      move.b   ciaddra(a0),d0              ; get DDR value
  509.      andi.b   #PRTMASK,d0                  ; save printer bits
  510.      ori.b      #~PRTMASK,d0                  ; set serial bits to output
  511.      move.b   d0,ciaddra(a0)              ; store value
  512.      ;
  513.      move.b   (a0),d0                  ; get PR value ( ciapra(a0) )
  514.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  515.      move.l   vb_SavePRA(a5),d1              ; get saved value
  516.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  517.      or.b      d1,d0                   ; combine the two
  518.      move.b   d0,(a0)                  ; and store ( ciapra(a0) )
  519.      ;
  520.      move.b   ciaddra(a0),d0              ; get DDR value
  521.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  522.      move.l   vb_SaveDDRA(a5),d1              ; get saved value
  523.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  524.      or.b      d1,d0                   ; combine the two
  525.      move.b   d0,ciaddra(a0)              ; and store
  526.      ;
  527.      lea      timerReq(pc),a1              ; get ptr to timer request
  528.      jsr      _LVOCloseDevice(a6)              ; go close it
  529.      ;
  530.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  531.      moveq      #MR_SERIALBITS,d0              ; set unit
  532.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  533.      ;
  534.      moveq      #MR_SERIALPORT,d0              ; set unit
  535.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  536.      movea.l  SysBase(pc),a6              ; restore ExecBase
  537.      ;
  538.     IFNE DANGER
  539.      subq.b   #1,IDNestCnt(a6)
  540.      bge.b      enable$
  541.      move.w   #$0C000,_intena
  542. enable$
  543.     ELSE
  544.      jsr      _LVOEnable(a6)              ; enable interrupts
  545.     ENDC
  546.      ;
  547.      subq.l   #1,vb_Initialized(a5)           ; clear flag
  548.      moveq      #0,d0                   ; free up everything
  549.      ;
  550. 10$
  551.      movea.l  a5,a6                   ; restore base
  552.      ;
  553.      movem.l  (sp)+,a1/a5                  ; restore registers
  554.      ;
  555.      tst.l      d0                      ; freed?
  556.      bne.b      dev_Null                  ; nope, can't expunge, exit
  557.      ;
  558.      clr.l      vb_SerFlags(a6)              ; clear flags
  559.      ;
  560.      btst      #LIBB_DELEXP,LIB_FLAGS(a6)
  561.      beq.b      dev_Null
  562.      ;
  563.      ;      Device Expunge routine (also fall through from dev_Close)
  564.      ;
  565. dev_Expunge:
  566.      ori.b      #1<<LIBB_DELEXP,LIB_FLAGS(a6)       ; Set expunge flag
  567.      tst.w      LIB_OPENCNT(a6)              ; currently open?
  568.      bne.b      dev_Null                  ; yep, so just exit
  569.      ;
  570.      move.l   vb_SegList(a6),d0              ; get seglist ptr
  571.      ;
  572.      movea.l  a6,a1                   ; get base
  573.      ; This is the REMOVE macro
  574.      move.l   (a1)+,a0
  575.      move.l   (a1),a1                  ; LN_PRED
  576.      move.l   a0,(a1)
  577.      move.l   a1,LN_PRED(a0)
  578.      ;
  579.      movea.l  a6,a1                   ; get base
  580.      movem.l  d0/a6,-(sp)                  ; save registers (save D0!)
  581.      moveq      #0,d0                   ; clear work
  582.      move.w   LIB_NEGSIZE(a6),d0              ; calculate
  583.      suba.l   d0,a1                   ;   memory address
  584.      add.w      LIB_POSSIZE(a6),d0              ;     and size
  585.      movea.l  SysBase(pc),a6              ; get ExecBase
  586.      jsr      _LVOFreeMem(a6)              ; free it
  587.      ;
  588.      movem.l  (sp)+,d0/a6
  589.      rts                          ; return (seglist in D0!)
  590.      ;
  591.      ;      Device "ExtFunc" routine
  592.      ;
  593.      cnop 0,4 ; align for 020+
  594. dev_Null:
  595.      moveq      #0,d0                   ; set return code
  596.      rts                          ; return
  597.      ;
  598.      ;
  599.      ;
  600.      ;
  601.      cnop 0,4 ; align for 020+
  602. cmdTable dc.l      cmd_Invalid-cmdTable              ; CMD_INVALID
  603.      dc.l      cmd_Reset-cmdTable              ; CMD_RESET
  604.      dc.l      cmd_Read-cmdTable              ; CMD_READ
  605.      dc.l      cmd_Write-cmdTable              ; CMD_WRITE
  606.      dc.l      cmd_Invalid-cmdTable              ; CMD_UPDATE
  607.      dc.l      cmd_Clear-cmdTable              ; CMD_CLEAR
  608.      dc.l      cmd_Invalid-cmdTable              ; CMD_STOP
  609.      dc.l      cmd_Invalid-cmdTable              ; CMD_START
  610.      dc.l      cmd_Flush-cmdTable              ; CMD_FLUSH
  611.      dc.l      sdcmd_Query-cmdTable              ; SDCMD_QUERY   (CMD_NONSTD)
  612.      dc.l      sdcmd_Break-cmdTable              ; SDCMD_BREAK
  613.      dc.l      sdcmd_SetParams-cmdTable          ; SDCMD_SETPARAMS
  614. endTable
  615.      dc.l      nscmd_DeviceQuery-cmdTable          ; NSCMD_DEVICEQUERY
  616. nsendTable
  617.      ;
  618.      ;      Device BeginIO routine
  619.      ;
  620.      cnop 0,4 ; align for 020+
  621. dev_BeginIO:
  622.      move.l   a5,-(sp)                  ; save register
  623.      move.l   a6,a5                   ; save base
  624.      move.l   SysBase(pc),a6              ; get ExecBase
  625.      ;
  626.      move.b   #NT_MESSAGE,LN_TYPE(a1)          ; set type
  627.      clr.b      IO_ERROR(a1)                  ; clear error
  628.      ;
  629.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),IO_FLAGS(a1) ; clear flags
  630.      ;
  631.      moveq      #0,d0
  632.      move.w   IO_COMMAND(a1),d0              ; get command
  633.      lsl.l      #2,d0                   ; multiply by 4
  634.      moveq      #endTable-cmdTable,d1
  635.      cmp.l      d1,d0                   ; in range?
  636.      bcs.b      5$                      ; yip, branch
  637.      cmpi.l   #(4*NSCMD_DEVICEQUERY),d0
  638.      bne.b      30$
  639.      move.l   d1,d0
  640.      ;
  641. 5$     move.l   cmdTable(pc,d0.l),d0              ; get routine offset
  642.      jsr      cmdTable(pc,d0.l)              ; go do it
  643.      tst.l      d0                      ; I/O completed?
  644.      bne.b      19$                      ; nope, go return
  645.      ;
  646. 10$
  647.      btst      #IOB_QUICK,IO_FLAGS(a1)          ; need to reply?
  648.      bne.b      20$                      ; nope, branch
  649. 15$     jsr      _LVOReplyMsg(a6)              ; send it back
  650.      ;
  651. 19$     andi.b   #~(1<<IOB_QUICK),IO_FLAGS(a1)       ; clear quick bit
  652. 20$     move.l   a5,a6                   ; restore base
  653.      move.l   (sp)+,a5                  ; restore register
  654.      rts                          ; return
  655.      cnop 0,4 ; align for 020+
  656.      ;
  657. 30$     move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; invalid command
  658.      bra.b      10$                      ; branch
  659.      ;
  660.      ;      Device AbortIO routine
  661.      ;
  662.      cnop 0,4 ; align for 020+
  663. dev_AbortIO:
  664.      move.l   a5,-(sp)                  ; save registers
  665.      movea.l  a6,a5                   ; save base
  666.      movea.l  SysBase(pc),a6              ; get ExecBase
  667.      ;
  668.     IFNE DANGER
  669.      move.w   #$04000,_intena
  670.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  671.     ELSE
  672.      jsr      _LVODisable(a6)              ; disable interrupts
  673.     ENDC
  674.      ;
  675.      move.l   IO_COMMAND(a1),d1              ; get flags
  676.      btst      #IOSERB_QUEUED+8,d1              ; queued request?
  677.      bne.b      40$                      ; yep, branch
  678.      ;
  679.      btst      #IOSERB_ACTIVE+8,d1              ; active request?
  680.      beq.b      10$                      ; nope, just exit
  681.      ;
  682.      move.l   d1,d0                   ; get command
  683.      swap      d0
  684.      subq.w   #CMD_READ,d0                  ; was it a read?
  685.      beq.b      20$                      ; yep, go process
  686.      ;
  687.      subq.w   #CMD_WRITE-CMD_READ,d0          ; was it a write?
  688.      beq.b      30$                      ; yep, go process
  689.      ;
  690.      subq.w   #SDCMD_BREAK-CMD_WRITE,d0          ; was it a break?
  691.      beq.b      30$                      ; yep, go process
  692.      ;
  693.      ;      Fall through or enter from below
  694.      ;
  695.     IFNE DANGER
  696. 10$     subq.b   #1,IDNestCnt(a6)
  697.      bge.b      enable$
  698.      move.w   #$0C000,_intena
  699. enable$
  700.     ELSE
  701. 10$     jsr      _LVOEnable(a6)              ; enable ints and return
  702.     ENDC
  703.      move.l   a5,a6                   ; restore base
  704.      move.l   (sp)+,a5                  ; restore registers
  705.      rts                          ; return
  706.      ;
  707.      ;      Abort an active read request
  708.      ;
  709. 20$     clr.l      cr_IOReq                  ; no longer active
  710.      bra.b      50$                      ; go set flags
  711.      ;
  712.      ;      Abort an active write request
  713.      ;
  714.      cnop 0,4 ; align for 020+
  715. 30$     clr.l      cw_Length                  ; no longer active
  716.      clr.l      cw_IOReq                  ; no longer active
  717.      move.l   cw_Buffer(pc),d0              ; get buffer ptr
  718.      sub.l      IO_DATA(a1),d0              ; calc number of bytes xfer'd
  719.      move.l   d0,IO_ACTUAL(a1)              ; store
  720.      ;
  721.      ;      Force a TBE interrupt to get the next write going.
  722.      ;
  723.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  724.      bra.b      50$                      ; go set flags
  725.      ;
  726.      ;      Remove I/O from queue.
  727.      ;
  728.      cnop 0,4 ; align for 020+
  729. 40$     move.l   a1,-(sp)                  ; save ptr
  730.      ; This is the REMOVE macro
  731.      move.l   (a1)+,a0
  732.      move.l   (a1),a1                  ; LN_PRED
  733.      move.l   a0,(a1)
  734.      move.l   a1,LN_PRED(a0)
  735.      ;
  736.      move.l   (sp)+,a1                  ; restore ptr
  737.      ;
  738.      ;      Set error and return I/O
  739.      ;
  740. 50$     move.b   #IOERR_ABORTED,IO_ERROR(a1)          ; set error code (2)
  741.      move.b   IO_FLAGS(a1),d1              ; get flags
  742.      ori.b      #1<<IOSERB_ABORT,d1              ; set abort flag
  743.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),d1  ; clear flags
  744.      move.b   d1,IO_FLAGS(a1)              ; store flags
  745.      btst      #IOB_QUICK,d1               ; need to reply?
  746.      bne.b      10$                      ; nope, branch
  747.      jsr      _LVOReplyMsg(a6)              ; send it back
  748.      bra.b      10$                      ; branch to return
  749.      ;
  750.      ;      Abort all active/queued commands and reset internal state
  751.      ;
  752.      cnop 0,4 ; align for 020+
  753. cmd_Reset:
  754.      move.l   a1,-(sp)                  ; save I/O request
  755.      bsr.b      cmd_Flush                  ; go abort queued requests
  756.      ;
  757.     IFNE DANGER
  758.      move.w   #$04000,_intena
  759.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  760.     ELSE
  761.      jsr      _LVODisable(a6)              ; disable interrupts
  762.     ENDC
  763.      ;
  764.      ;      This must follow the DISABLE
  765.      ;
  766.      exg      a5,a6                   ; exchange base and ExecBase
  767.      ;
  768.      move.l   cr_IOReq(pc),d0              ; active read?
  769.      beq.b      10$                      ; nope, branch
  770.      movea.l  d0,a1                   ; get I/O request
  771.      bsr      dev_AbortIO                  ; go abort it
  772.      ;
  773.      ;
  774.      ;
  775. 10$     move.l   cw_IOReq(pc),d0              ; active write?
  776.      beq.b      20$                      ; nope, branch
  777.      movea.l  d0,a1                   ; get I/O request
  778.      bsr      dev_AbortIO                  ; go abort it
  779.      ;
  780. 20$     exg      a5,a6                   ; restore base and ExecBase
  781.      move.l   (sp)+,a1                  ; restore I/O request
  782.      moveq      #8,d0                   ; get char size
  783.      move.b   d0,IO_READLEN(a1)              ; set read length
  784.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  785.      moveq      #1,d0                   ; get stop bits
  786.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  787.      move.l   vb_DefBaud(a5),IO_BAUD(a1)          ; set to default baud
  788.      move.l   vb_DefRBufLen(a5),IO_RBUFLEN(a1)    ; set to default buflen
  789.      bsr      internalReset               ; go set/verify parameters
  790.      move.b   d0,IO_ERROR(a1)              ; set error code
  791.      ;
  792.      ;      Set RC
  793.      ;
  794.      moveq      #0,d0                   ; I/O complete
  795.     IFNE DANGER
  796.      subq.b   #1,IDNestCnt(a6)
  797.      bge.b      enable$
  798.      move.w   #$0C000,_intena
  799. enable$  rts
  800.     ELSE
  801.      jmp      _LVOEnable(a6)              ; enable interrupts
  802.                               ; return
  803.     ENDC
  804.      ;
  805.      ;      Abort all "queued" requests, leaving all active alone.
  806.      ;
  807.      cnop 0,4 ; align for 020+
  808. cmd_Flush:
  809.      movem.l  a1/a2,-(sp)
  810.      ;
  811.      ;      Abort all queued read requests.
  812.      ;
  813.      lea      readQ(pc),a2                  ; get ptr to read queue
  814.      bsr.b      20$                      ; branch to abort
  815.      ;
  816.      ;      Abort all queued write requests.
  817.      ;
  818.      lea      writeQ(pc),a2               ; get ptr to write queue
  819.      bsr.b      20$                      ; branch to abort
  820.      ;
  821.      ;      restore, and return to caller
  822.      ;
  823.      movem.l  (sp)+,a1/a2
  824.      ;
  825.      ;      Set RC and return
  826.      ;
  827.      moveq      #0,d0                   ; I/O complete
  828.      ;
  829.      ;      !!!NOTE!!!  In a mad attempt to save 2 bytes, this RTS is used
  830.      ;      by the subroutine below.  Why waste 'em?  B-)
  831.      ;
  832. 10$     rts                          ; return ( used below too!! )
  833.      ;
  834.      ;      Subroutine to remove and reply each I/O request.
  835.      ;
  836.      cnop 0,4 ; align for 020+
  837.     IFNE DANGER
  838. 20$     move.l   #custom+intena,a0
  839.      move.w   #$04000,(a0)
  840.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  841.     ELSE
  842. 20$     jsr      _LVODisable(a6)              ; disable interrupts
  843.     ENDC
  844.      move.l   a2,8(a2)                  ; clear list
  845.      move.l   (a2)+,a1                  ; and get ptr to first I/O request
  846.      move.l   a2,-(a2)                  ;
  847.      move.l   a1,a2                   ; save I/O request in a2
  848.      ;
  849.     IFNE DANGER
  850.      subq.b   #1,IDNestCnt(a6)
  851.      bge.b      enable$
  852.      move.w   #$0C000,(a0)
  853. enable$
  854.     ELSE
  855.      jsr      _LVOEnable(a6)              ; enable interrupts
  856.     ENDC
  857.      ;
  858. 25$     move.l   (a2),d0                  ; get next I/O request
  859.      beq.b      10$                      ; yep, branch to return
  860.      ;
  861.      move.l   a2,a1                   ; move I/O request to a1
  862.      move.l   d0,a2                   ; save next I/O request in a2
  863.      andi.b   #~(1<<IOSERB_QUEUED),IO_FLAGS(a1)   ; no longer queued
  864.      moveq      #IOERR_ABORTED,d0              ; indicate aborted
  865.      move.b   d0,IO_ERROR(a1)              ; store status
  866.      ;
  867.      jsr      _LVOReplyMsg(a6)              ; send it back
  868.      bra.b      25$                      ; continue with next
  869.      ;
  870.      ;      Process a CMD_READ request.
  871.      ;
  872.      cnop 0,4 ; align for 020+
  873. cmd_Read:
  874.      ;
  875.      ;      Zero length requests just get returned.
  876.      ;
  877.      clr.l      IO_ACTUAL(a1)               ; clear bytes read
  878.      move.l   IO_LENGTH(a1),d0              ; get length and test
  879.      beq.b      20$                      ; yep, leave
  880.      ;
  881.      ;      This can be used to circumvent a bug in NComm 3.0 which
  882.      ;      references the buffer even when there was nothing read.
  883.      ;
  884.     IFNE     NCOMM
  885.      move.l   IO_DATA(A1),a0              ; get data pointer
  886.      clr.b      (a0)                      ; clear first byte in buffer
  887.     ENDC
  888.      ;
  889.      ;      The disable counter works just like exec's TDNestCnt field.  It's
  890.      ;      initialized to -1.  After incrementing, if it is 0, then we
  891.      ;      can attempt to process this request immediately.  If it's > 0,
  892.      ;      then we're already disabled and we must queue this request.
  893.      ;
  894.      addq.l   #1,disableRead              ; incr disable count
  895.      bgt.b      50$                      ; >0, already disabled
  896.      ;
  897.      ;      If we're already processing an request, this one has to wait
  898.      ;      until that one is done, so go queue it.
  899.      ;
  900.      tst.l      cr_IOReq(pc)                  ; have an active request?
  901.      bne.b      50$                      ; yep, go queue this one
  902.      ;
  903.      ;      If we don't have enough bytes to satisfy this request then go
  904.      ;      queue it.
  905.      ;
  906.      cmp.l      i_InCnt(pc),d0              ; length > current bytes?
  907.      bgt.b      50$                      ; yep, go queue it
  908.      ;
  909.      ;      Setup fields and go copy the data
  910.      ;
  911.     IFNE NCOMM
  912.      move.l   a0,cr_OutPtr                  ; get/set output ptr
  913.     ELSE
  914.      move.l   IO_DATA(a1),cr_OutPtr           ; get/set output ptr
  915.     ENDC
  916.      move.l   d0,cr_Length                  ; get/set output count
  917.      bsr      copyData                  ; go copy 'em
  918.      moveq      #0,d0                   ; I/O complete
  919.      ;
  920.      ;      We're done, so back off the disable counter.
  921.      ;
  922. 10$     subq.l   #1,disableRead              ; decr disable count
  923.      ;
  924.      ;      Return to caller
  925.      ;
  926. 20$     rts                          ; return
  927.      ;
  928.      ;      Just set flags and queue.  The read interrupt will handle it.
  929.      ;
  930. 50$     ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)    ; indicate queued
  931.      ;
  932.      ;      Add this request to the end.
  933.      ;
  934.     IFNE DANGER
  935.      move.w   #$04000,_intena
  936.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  937.     ELSE
  938.      jsr      _LVODisable(a6)              ; disable interrupts
  939.     ENDC
  940.      lea      readQ(pc),a0                  ; get pointer to read queue
  941.      ; This is the ADDTAIL macro
  942.      addq.l   #LH_TAIL,a0
  943.      move.l   LN_PRED(a0),d0
  944.      move.l   a1,LN_PRED(a0)
  945.      EXG      d0,a0
  946.      movem.l  d0/a0,(a1)
  947.      move.l   a1,(a0)
  948.      ;
  949.      ;
  950.      ;      Indicate that this request was not handled immediatly.
  951.      ;
  952.      moveq      #1,d0                   ; I/O not complete
  953.     IFNE DANGER
  954.      subq.b   #1,IDNestCnt(a6)
  955.      bge.b      10$
  956.      move.w   #$0C000,_intena
  957.     ELSE
  958.      jsr      _LVOEnable(a6)              ; enable interrupts
  959.     ENDC
  960.      bra.b      10$                      ; branch to return
  961.      ;
  962.      ;      Process a CMD_WRITE request.
  963.      ;
  964.      cnop 0,4 ; align for 020+
  965. cmd_Write:
  966.      clr.l      IO_ACTUAL(a1)               ; clear bytes written
  967.      move.l   IO_LENGTH(a1),d0              ; get length
  968.      bne.b      10$                      ; nope, proccess
  969.      rts
  970.      cnop 0,4 ; align for 020+
  971. 10$
  972.     IFNE EOFCODE
  973.      btst      #SERB_EOFMODE,IO_SERFLAGS(a1)       ; EOFMODE requested?
  974.      beq.b      20$                      ; nope, branch
  975.      moveq      #SerErr_InvParam,d0
  976.      move.b   d0,IO_ERROR(a1)              ; Set error
  977.      moveq      #0,d0                   ; I/O Complete
  978.      rts
  979.      cnop 0,4 ; align for 020+
  980. 20$
  981.     ENDC
  982.     IFNE NEGCHECK
  983.      moveq      #-1,d1                  ; get value
  984.      cmp.l      d1,d0                   ; length = -1?
  985.      bne.b      sdcmd_Break                  ; nope, skip scan
  986.      ;
  987.      ;      Scan the data for null to calc the length
  988.      ;
  989.      move.l   a0,d1                   ; save data ptr
  990. 30$     tst.b      (a0)+                   ; does it equal 0? (first byte)
  991.      beq.b      40$                      ; yep, finish
  992.      tst.b      (a0)+                   ; does it equal 0? (second byte)
  993.      beq.b      40$                      ; yep, finish
  994.      tst.b      (a0)+                   ; does it equal 0? (third byte)
  995.      beq.b      40$                      ; yep, finish
  996.      tst.b      (a0)+                   ; does it equal 0? (fourth byte)
  997.      bne.b      30$                      ; nope, loop
  998. 40$     move.l   a0,d0
  999.      sub.l      d1,d0                   ; calc # of bytes
  1000.      move.l   d0,IO_LENGTH(a1)              ; save length
  1001.      bra.b      sdcmd_Break
  1002.      nop
  1003.     ENDC
  1004.      ;
  1005.      ;      Entry point for Break command and fall through from cmd_Write.
  1006.      ;
  1007.      cnop 0,4 ; align for 020+
  1008. sdcmd_Break:
  1009.      ;
  1010.      ;      Just set flags and queue. The TBE interrupt will handle it.
  1011.      ;
  1012.      ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)     ; indicate queued
  1013.      ;
  1014.      ;      Protect.
  1015.      ;
  1016.     IFNE DANGER
  1017.      move.w   #$04000,_intena
  1018.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  1019.     ELSE
  1020.      jsr      _LVODisable(a6)             ; disable interrupts
  1021.     ENDC
  1022.      ;
  1023.      ;      Add request to end of queue.
  1024.      ;
  1025.      lea      writeQ(pc),a0               ; get queue list ptr
  1026.      ; This is the ADDTAIL macro
  1027.      addq.l   #LH_TAIL,a0
  1028.      move.l   LN_PRED(a0),d0
  1029.      move.l   a1,LN_PRED(a0)
  1030.      EXG      d0,a0
  1031.      movem.l  d0/a0,(a1)
  1032.      move.l   a1,(a0)
  1033.      ;
  1034.      ;      If we have an active request, don't force interrupt.
  1035.      ;
  1036.      tst.l      cw_IOReq(pc)                  ; have an active request?
  1037.      bne.b      10$                      ; yep, branch
  1038.      ;
  1039.      ;      Force a TBE interrupt to get the writes going.
  1040.      ;
  1041.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  1042.      ;
  1043.      ;      Enable, set RC and return to caller.
  1044.      ;
  1045. 10$     moveq      #1,d0                   ; I/O not complete
  1046.     IFNE DANGER
  1047.      subq.b   #1,IDNestCnt(a6)
  1048.      bge.b      enable$
  1049.      move.w   #$0C000,_intena
  1050. enable$  rts
  1051.     ELSE
  1052.      jmp      _LVOEnable(a6)              ; enable interrupts
  1053.                               ; return
  1054.     ENDC
  1055.      ;
  1056.      ;      Resets serial read buffer
  1057.      ;
  1058.      ;      Since this routine is called internally, it must NOT reference
  1059.      ;      the I/O request.
  1060.      ;
  1061.      cnop 0,4 ; align for 020+
  1062. cmd_Clear:
  1063.     IFNE DANGER
  1064.      move.w   #$04000,_intena
  1065.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  1066.     ELSE
  1067.      jsr      _LVODisable(a6)              ; disable interrupts
  1068.     ENDC
  1069.      ;
  1070.      ;      Load registers
  1071.      ;
  1072.      move.l   vb_CurRBuf(a5),d0              ; get internal buffer ptr
  1073.      move.l   vb_CurRBufLen(a5),d1              ; and internal buffer len
  1074.      lea      i_BufPtr(pc),a0              ; get ptr internal control
  1075.      ;
  1076.      ;      Initialize global buffer variables
  1077.      ;
  1078.      move.l   d0,(a0)+                  ; store buffer ptr
  1079.      move.l   d0,(a0)+                  ; set current input ptr
  1080.      move.l   d0,(a0)+                  ; set current output ptr
  1081.      add.l      d1,d0                   ; add buffer length
  1082.      move.l   d0,(a0)+                  ; store ptr to end of buffer
  1083.      clr.l      (a0)+                   ; clear byte cnt
  1084.      move.l   vb_CurBaud(a5),d0              ; get internal baud
  1085.      lsr.l      #4,d0                   ; divide by 16
  1086.      sub.l      d0,d1                   ; subtract from length
  1087.      move.l   d1,(a0)                  ; set threshold
  1088.      ;
  1089.      ;      Enable, set RC and return.
  1090.      ;
  1091.      moveq      #0,d0                   ; I/O complete
  1092.     IFNE DANGER
  1093.      subq.b   #1,IDNestCnt(a6)
  1094.      bge.b      enable$
  1095.      move.w   #$0C000,_intena
  1096. enable$  rts
  1097.     ELSE
  1098.      jmp      _LVOEnable(a6)              ; enable interrupts
  1099.                               ; return
  1100.     ENDC
  1101.      ;
  1102.      ;
  1103.      ;
  1104.      cnop 0,4 ; align for 020+
  1105. cmd_Invalid
  1106.      move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; set bad status
  1107.      moveq      #0,d0                   ; I/O complete
  1108.      rts                          ; return
  1109.      ;
  1110.      ;      Returns number of bytes currently in internal buffer and
  1111.      ;      current serial port status.
  1112.      ;
  1113.      ;      NOTE:  Not completely compatible with standard serial.device
  1114.      ;      since it doesn't return the upper byte of IO_STATUS.
  1115.      ;
  1116.      cnop 0,4 ; align for 020+
  1117. sdcmd_Query:
  1118.     IFNE DANGER
  1119.      move.l   #custom+intena,a0
  1120.      move.w   #$04000,(a0)
  1121.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  1122.     ELSE
  1123.      jsr      _LVODisable(a6)              ; disable interrupts
  1124.     ENDC
  1125.      moveq      #0,d0                   ; clear d0
  1126.      move.b   _ciabpra,d0                  ; get PR register
  1127.      andi.b   #~PRTMASK,d0                  ; zap printer bits
  1128.      ;
  1129.      ;      To use this Set SETDSR to one.  This was done
  1130.      ;      for a user whose DSR pin did not function.
  1131.      ;
  1132.     IFNE SETDSR
  1133.      andi.b     #~(1<<CIAB_COMDSR),d0              ; set DSR
  1134.     ENDC
  1135.      ;
  1136.      ;
  1137.      ;
  1138.      move.w   d0,IO_STATUS(a1)              ; store status
  1139.      move.l   i_InCnt(pc),IO_ACTUAL(a1)          ; byte left in buffer
  1140.      ;
  1141.      ;      Enable, set RC and return.
  1142.      ;
  1143.      moveq      #0,d0                   ; I/O complete
  1144.     IFNE DANGER
  1145.      subq.b   #1,IDNestCnt(a6)
  1146.      bge.b      enable$
  1147.      move.w   #$0C000,(a0)
  1148. enable$  rts
  1149.     ELSE
  1150.      jmp      _LVOEnable(a6)              ; enable interrupts
  1151.                               ; return
  1152.     ENDC
  1153.      ;
  1154.      ;
  1155.      ;
  1156.      cnop 0,4 ; align for 020+
  1157. sdcmd_SetParams:
  1158.      ;
  1159.      ;      Validate the read, write, and stop bit lengths.
  1160.      ;
  1161.      moveq      #8,d0                   ; get char length
  1162.      cmp.b      IO_READLEN(a1),d0              ; 8 bit chars for read?
  1163.      bne.b      40$                      ; nope, branch
  1164.      cmp.b      IO_WRITELEN(a1),d0              ; 8 bit chars for write?
  1165.      bne.b      40$                      ; nope, branch
  1166.      moveq      #1,d0                   ; get stop bits
  1167.      cmp.b      IO_STOPBITS(a1),d0              ; 1 stop bit?
  1168.      bne.b      40$                      ; nope, branch
  1169.      ;
  1170.      ;      Get and validate the baud rate.
  1171.      ;
  1172.      move.l   IO_BAUD(a1),d1              ; get baud from I/O req
  1173.      bne.b      20$                      ; specified?
  1174.      move.l   vb_CurBaud(a5),d1              ; get current baud from base
  1175.      bne.b      20$                      ; specified?
  1176.      move.l   vb_DefBaud(a5),d1              ; get default baud from base
  1177. 20$     cmp.l      #110,d1                  ; too low?
  1178.      blt.b      40$                      ; error
  1179.      cmp.l      #292000,d1                  ; too high?
  1180.      bgt.b      40$                      ; error
  1181.      ;
  1182.      ;      Get and validate the buffer length.
  1183.      ;
  1184.      move.l   IO_RBUFLEN(a1),d0              ; get buffer length
  1185.      bne.b      30$                      ; specified?
  1186.      move.l   vb_CurRBufLen(a5),d0              ; get current from base
  1187.      bne.b      30$                      ; specified?
  1188.      move.l   vb_DefRBufLen(a5),d0              ; get default from base
  1189.      ;
  1190. 30$     bsr.b      internalReset               ; go init baud and buffer
  1191. 35$     move.b   d0,IO_ERROR(a1)              ; set error code
  1192.      bne.b      39$
  1193.      ;
  1194.      ;      If the 7wire bit is not on, we will only use 3-wire protocol
  1195.      ;
  1196.      moveq      #0,d0                   ; clear flag
  1197.      btst      #SERB_7WIRE,IO_SERFLAGS(a1)          ; use 7wire handshaking?
  1198.      beq.b      36$                      ; nope, branch
  1199.      moveq      #1,d0                   ; set flag
  1200. 36$     move.l   d0,Handshake                  ; store flag
  1201.      ;
  1202.      ;      Set RC and return.
  1203.      ;
  1204. 39$     moveq      #0,d0                   ; I/O complete
  1205.      rts                          ; return
  1206.      ;
  1207.      ;      Invalid parm detected.
  1208.      ;
  1209. 40$     moveq      #SerErr_InvParam,d0              ; set error
  1210.      bra.b      35$                      ; go return
  1211.      ;
  1212.      ;  The NewStyleDevices Query command.
  1213.      ;
  1214.      cnop 0,4 ; align for 020+
  1215. nscmd_DeviceQuery:
  1216.      move.l   IO_DATA(a1),d0
  1217.      beq.b      10$
  1218.      move.l   d0,a0
  1219.      moveq      #NSDEVICEQUERYRESULT_SIZE,d0
  1220.      move.l   d0,SIZEAVAILABLE(a0)
  1221.      move.l   d0,IO_ACTUAL(a1)
  1222.      move.w   #NSDEVTYPE_SERIAL,DEVICETYPE(a0)
  1223.      move.l   a1,-(sp)
  1224.      lea      cmdlist(pc),a1
  1225.      move.l   a1,SUPPORTEDCOMMANDS(a0)
  1226.      move.l   (sp)+,a1
  1227. 10$     moveq      #0,d0
  1228.      rts
  1229.      ;
  1230.      ;      Reset the buffer and baud rate
  1231.      ;
  1232.      ;      Registers:  D0 = Buffer length
  1233.      ;              D1 = Baud rate
  1234.      ;
  1235.      cnop 0,4 ; align for 020+
  1236. internalReset:
  1237.      ;
  1238.      ;      Disable interrupts.
  1239.      ;
  1240.     IFNE DANGER
  1241.      move.w   #$04000,_intena
  1242.      addq.b   #1,IDNestCnt(a6)              ; disable interrupts
  1243.     ELSE
  1244.      jsr      _LVODisable(a6)              ; disable interrupts
  1245.     ENDC
  1246.      ;
  1247.      ;      Save buffer length and go set serper.
  1248.      ;
  1249.      move.l   d0,-(sp)                  ; save D0
  1250.      move.l   d1,d0                   ; get baud rate
  1251.      ;
  1252.      ;      Set serial period register
  1253.      ;
  1254.      cmp.l      vb_CurBaud(a5),d0              ; current baud = new baud?
  1255.      beq.b      40$                      ; yep, just exit
  1256.      move.l   d0,vb_CurBaud(a5)              ; save new baud
  1257.      move.l   d0,d1                   ; save again
  1258.      lsl.l      #3,d0                   ; baud *= 8
  1259.      sub.l      d1,d0                   ; baud -= saved baud
  1260.      move.l   #25000000,d1                  ; get NTSC base
  1261.      cmpi.b   #50,PowerSupplyFrequency(a6)          ; PAL machine?
  1262.      bne.b      5$                      ; nope, branch
  1263.      move.l   #24772416,d1                  ; get PAL base
  1264. 5$     cmp.l      #$FFFF,d0                  ; Divide
  1265.      ble.b      20$                      ;
  1266.      lsr.l      #5,d0                   ;
  1267.     IFNE NEWCODE
  1268.      divu.l   d0,d1                   ;
  1269.     ELSE
  1270.      divu.w   d0,d1                   ;
  1271.      andi.l   #$FFFF,d1                  ;
  1272.     ENDC
  1273.      lsr.l      #5,d1                   ;
  1274.      bra.b      30$                      ;
  1275.      cnop 0,4 ; align for 020+
  1276. 20$     divu.w   d0,d1                   ;
  1277. 30$     move.w   d1,_serper                  ; set period value
  1278. 40$
  1279.      move.l   (sp)+,d0                  ; restore D0
  1280.      ;
  1281.      ;      Determine if the buffer length is adequate for the selected CPS.
  1282.      ;      If not, use 64K for the length.
  1283.      ;
  1284.      move.l   vb_CurBaud(a5),d1              ; get current baud
  1285.      lsr.l      #2,d1                   ; divide by 4
  1286.      cmp.l      d1,d0                   ; buflen > CPS
  1287.      bhi.b      10$                      ; yep, branch
  1288.      move.l   #65536,d0                  ; else use 64K
  1289.      ;
  1290.      ;      Allocate new internal buffer
  1291.      ;
  1292. 10$     cmp.l      vb_CurRBufLen(a5),d0              ; len same as previous?
  1293.      beq.b      11$                      ; yep, so no need to alloc
  1294.      move.l   d0,d1                   ; save length
  1295.      movem.l  d1/a1,-(sp)
  1296.      moveq      #MEMF_PUBLIC|MEMF_FAST,d1          ; public memory
  1297.      jsr      _LVOAllocMem(a6)              ; go allocate it
  1298.      movem.l  (sp)+,d1/a1
  1299.      tst.l      d0                      ; did we get it?
  1300.      beq.b      21$                      ; if zero, error
  1301.      move.l   d0,-(sp)                  ; save d0
  1302.      bsr.b      freeBuf                  ; go free previous buffer
  1303.      move.l   (sp)+,vb_CurRBuf(a5)              ; store new ptr
  1304.      move.l   d1,vb_CurRBufLen(a5)              ; and length
  1305.      bsr      cmd_Clear                  ; go setup buffer
  1306. 11$     moveq      #0,d0                   ; success
  1307.      bra.b      22$                      ; return
  1308.      cnop 0,4 ; align for 020+
  1309. 21$     moveq      #SerErr_BufErr,d0              ; set error status
  1310. 22$
  1311.      ;
  1312.      ;      Enable and return to caller.
  1313.      ;
  1314.     IFNE DANGER
  1315.      subq.b   #1,IDNestCnt(a6)
  1316.      bge.b      enable$
  1317.      move.w   #$0C000,_intena
  1318. enable$  rts
  1319.     ELSE
  1320.      jmp      _LVOEnable(a6)              ; enable interrupts / return (D0 has status)
  1321.     ENDC
  1322.      ;
  1323.      ;      Free internal buffer
  1324.      ;
  1325.      cnop 0,4 ; align for 020+
  1326. freeBuf:
  1327.      move.l   vb_CurRBuf(a5),d0              ; is one there?
  1328.      beq.b      10$                      ; no so branch
  1329.      movem.l  d1/a1,-(sp)                  ; save registers
  1330.      movea.l  d0,a1                   ; get ptr
  1331.      move.l   vb_CurRBufLen(a5),d0              ; get length
  1332.      clr.l      vb_CurRBuf(a5)              ; clear
  1333.      clr.l      vb_CurRBufLen(a5)              ; clear
  1334.      jsr      _LVOFreeMem(a6)              ; free it
  1335.      movem.l  (sp)+,d1/a1
  1336. 10$     rts                          ; return
  1337.      ;
  1338.      ;      Checks CTS status and if clear generates a TBE interrupt or
  1339.      ;      requeues the timer request.
  1340.      ;
  1341.      ;      Entered from Exec using the MsgPort callback.
  1342.      ;
  1343.      ;      Input:   a6 = ExecBase
  1344.      ;      Output:  none
  1345.      ;
  1346.      ;      No need to preserve d0/d1/a0/a1
  1347.      ;
  1348.      cnop 0,4 ; align for 020+
  1349. timerRtn:
  1350.      lea      timerReq(pc),a1              ; get ptr to timer request
  1351.      ; This is the REMOVE macro
  1352.      move.l   (a1)+,a0
  1353.      move.l   (a1),a1                  ; LN_PRED
  1354.      move.l   a0,(a1)
  1355.      move.l   a1,LN_PRED(a0)
  1356.      ;
  1357.      ;      If we were breaking, reset adkcon.
  1358.      ;
  1359.      move.l   #custom+adkconr,a0              ; get ptr to custom regs
  1360.      btst      #ADKB_UARTBRK&7,(a0)              ; were we breaking?
  1361.      beq.b      10$                      ; nope, skip reset
  1362.      ;
  1363.      move.w   #ADKF_UARTBRK,adkcon-adkconr(a0)    ; stop breaking
  1364.      ;
  1365.      ;
  1366.      ;
  1367. 10$     tst.l      Handshake(pc)               ; are we handshaking?
  1368.      beq.b      20$                      ; nope, generate interrupt
  1369.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1370.      beq.b      20$                      ; yep, go start writing
  1371.      ;
  1372.      lea      timerReq(pc),a1              ; get ptr to timer request
  1373.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1374.      jmp      _LVOSendIO(a6)              ; go queue it / return
  1375.      ;
  1376.      ;      CTS is clear so generate TBE interrupt to restart writing
  1377.      ;
  1378.      cnop 0,4 ; align for 020+
  1379. 20$     move.w   #INTF_SETCLR|INTF_TBE,intreq-adkconr(a0)  ; set TBE interrupt
  1380.      rts                          ; return
  1381.      ;
  1382.      ;      Non serial interrupt
  1383.      ;      Handles:
  1384.      ;        INTB_DSKBLK
  1385.      ;        INTB_SOFTINT
  1386.      ;
  1387.      ;  a0 - custom chips base
  1388.      ;  a1 - is_Data
  1389.      ;
  1390.      cnop 0,4 ; align for 020+
  1391. level1n:
  1392.      movem.l  d1/a1/a5/a6,-(sp)              ; save registers
  1393.      move.l   d0,d1                   ; get enabled interrupts
  1394.      swap      d1
  1395.      move.l   SysBase(pc),a6              ; get ExecBase
  1396.      and.l      d0,d1                   ; and in requested interrupts
  1397.      ;
  1398.      btst      #INTB_DSKBLK,d1              ; Disk block done?
  1399.      beq.b      10$                      ; nope, branch
  1400.      ;
  1401.      movem.l  IVDSKBLK(a6),a1/a5              ; get data and code ptrs
  1402.      pea      20$(pc)                  ; push return address
  1403.      jmp      (a5)                      ; jump to routine
  1404.      ;
  1405. 10$
  1406.      ;
  1407.      movem.l  IVSOFTINT(a6),a1/a5              ; get data and code ptrs
  1408.      jsr      (a5)                      ; jump to routine
  1409.      ;
  1410. 20$
  1411.      movem.l  (sp)+,d1/a1/a5/a6              ; restore registers
  1412.      movem.l  (sp)+,d0/a0
  1413.      rte                          ; return
  1414.      ;
  1415.      ;    Level 1 interrupt handler
  1416.      ;    Handles:
  1417.      ;        INTB_TBE
  1418.      ;
  1419.      ;  a1 - is_Data
  1420.      ;
  1421.      cnop 0,4 ; align for 020+
  1422. level1:
  1423.      movem.l  d0/a0,-(sp)
  1424.      move.l   #custom,a0                  ; get ptr to custom regs
  1425.      move.l   intenar(a0),d0              ; get intenar & intreqr
  1426.      btst      #INTB_INTEN+16,d0              ; interrupts enabled?
  1427.      beq.b      30$                      ; nope, ignore
  1428.      btst      #INTB_TBE,d0                  ; xmit buffer empty?
  1429.      beq.b      level1n                  ; nope, invoke old handler
  1430.      ;
  1431.      ;      Handle "Transmit Buffer Empty" interrupt (write)
  1432.      ;
  1433.      move.w   #INTF_TBE,intreq(a0)          ; clear interrupt
  1434.      ;
  1435.      ;      If we're not handshaking, bypass it.
  1436.      ;
  1437. 10$     tst.l      Handshake(pc)               ; are we handshaking?
  1438.      beq.b      20$                      ; nope, skip CTS test
  1439.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1440.      bne.b      40$                      ; nope, branch
  1441.      ;
  1442.      ;      If cw_Length goes negative here, we are either done with a
  1443.      ;      request or we were called as a result of a fake interrupt
  1444.      ;      to force us to get the next request going.
  1445.      ;
  1446. 20$     subq.l   #1,cw_Length                  ; decr write length
  1447.      blt.b      60$                      ; < zero, done, branch
  1448.      ;
  1449.      ;      Currently processing a request.
  1450.      ;
  1451.      movea.l  cw_Buffer(pc),a0              ; get buffer ptr
  1452.      move.w   #256,d0                  ; set stop bit
  1453.      move.b   (a0)+,d0                  ; get next byte
  1454.      move.l   a0,cw_Buffer                  ; store buffer ptr
  1455.      move.w   d0,_serdat                  ; store in serdat reg
  1456.      ;
  1457. 30$     movem.l  (sp)+,d0/a0
  1458.      rte                          ; return
  1459.      ;
  1460.      ;      Queue a timer request to recheck CTS status
  1461.      ;
  1462.      cnop 0,4 ; align for 020+
  1463. 40$
  1464.      movem.l  d1/a1/a6,-(sp)
  1465.      lea      timerReq(pc),a1              ; get ptr to timer request
  1466.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1467.      movea.l  SysBase(pc),a6              ; get ExecBase
  1468.      jsr      _LVOSendIO(a6)              ; queue the request
  1469.      movem.l  (sp)+,d1/a1/a6
  1470.      bra.b      30$                      ; go return
  1471.      ;
  1472.      ;      There aren't anymore requests, so clear and exit
  1473.      ;
  1474.      cnop 0,4 ; align for 020+
  1475. 50$     clr.l      cw_Length-Start(a6)              ; clear length
  1476.      clr.l      cw_IOReq-Start(a6)              ; clear
  1477.      movem.l  (sp)+,d1/a1/a6
  1478.      bra.b      30$                      ; go return
  1479.      ;
  1480.      ;      Write request completed
  1481.      ;
  1482.      cnop 0,4 ; align for 020+
  1483. 60$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1484.      ;
  1485.      move.l   cw_IOReq(pc),d0              ; active I/O request?
  1486.      beq.b      70$                      ; nope, branch
  1487.      ;
  1488.      ;      Reply it and setup for next
  1489.      ;
  1490.      movea.l  d0,a1                   ; get I/O request
  1491.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1492.      clr.b      IO_ERROR(a1)                  ; no error
  1493.      movea.l  SysBase(pc),a6              ; get ExecBase
  1494.      jsr      _LVOReplyMsg(a6)              ; return I/O request
  1495.      ;
  1496. 70$     lea      Start(pc),a6                  ; get section base
  1497.      ;
  1498.      lea      writeQ(pc),a1               ; get ptr to write queue
  1499.      move.l   (a1),a0                  ; get head of list
  1500.      move.l   (a0),d0                  ; get successor
  1501.      beq.b      50$                      ; end of list? yep, branch
  1502.      ;
  1503.      ;      Remove the node from the list
  1504.      ;
  1505.      move.l   d0,(a1)                  ; make new head
  1506.      exg.l      d0,a0                   ; swap nodes
  1507.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1508.      ;
  1509.      move.l   d0,a1                   ; get I/O request
  1510.      ;
  1511.      ;      If it's a BREAK, then branch to process as such.
  1512.      ;
  1513.      cmpi.w   #SDCMD_BREAK,IO_COMMAND(a1)          ; BREAK command?
  1514.      beq.b      100$                      ; yep, branch
  1515.      ;
  1516.      ;      Check for absolute length
  1517.      ;
  1518.      move.l   IO_DATA(a1),a0              ; get data ptr
  1519.      move.l   IO_LENGTH(a1),d0              ; get length
  1520.      ;
  1521. 90$     move.l   d0,IO_ACTUAL(a1)              ; go ahead and set it
  1522.      move.b   IO_FLAGS(a1),d1
  1523.      move.l   d0,cw_Length-Start(a6)          ; store length
  1524.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1525.      move.l   a0,cw_Buffer-Start(a6)          ; store buffer ptr
  1526.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1527.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1528.      move.b   d1,IO_FLAGS(a1)
  1529.      ;
  1530.      movem.l  (sp)+,d1/a1/a6
  1531.      bra      10$                      ; go start request
  1532.      ;
  1533.      ;      Start the BREAK.
  1534.      ;
  1535.      cnop 0,4 ; align for 020+
  1536. 100$     move.b   IO_FLAGS(a1),d1
  1537.      clr.l      cw_Length-Start(a6)              ; clear length
  1538.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1539.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1540.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1541.      move.w   #ADKF_SETCLR|ADKF_UARTBRK,_adkcon   ; start break
  1542.      move.b   d1,IO_FLAGS(a1)
  1543.      move.l   IO_BRKTIME(a1),d0              ; get break time
  1544.      lea      timerReq(pc),a1              ; get ptr to timer request
  1545.      move.l   d0,IOTV_TIME+TV_MICRO(a1)          ; set the timeout
  1546.      movea.l  SysBase(pc),a6              ; get ExecBase
  1547.      jsr      _LVOSendIO(a6)              ; queue the request
  1548.      movem.l  (sp)+,d1/a1/a6
  1549.      bra      30$                      ; go exit
  1550.      ;
  1551.      ;    Non serial interrupt
  1552.      ;    Handles:
  1553.      ;        INTB_DSKSYNC
  1554.      ;
  1555.      ;  a0 - custom chips base
  1556.      ;
  1557.      cnop 0,4 ; align for 020+
  1558. level5n:
  1559.      movem.l  d1/a1/a5/a6,-(sp)              ; save registers
  1560.      move.l   d0,d1
  1561.      move.l   SysBase(pc),a6              ; get ExecBase
  1562.      swap      d1
  1563.      movem.l  IVDSKSYNC(A6),a1/a5              ; get data and code ptrs
  1564.      and.l      d0,d1
  1565.      move.l   #custom,a0                  ; get ptr to custom regs
  1566.      jsr      (a5)                      ; branch to routine
  1567.      movem.l  (sp)+,d1/a1/a5/a6              ; restore registers
  1568.      movem.l  (sp)+,d0/a0
  1569.      rte                          ; return
  1570.      ;
  1571.      ;    Default Level 5 handler
  1572.      ;    Handles:
  1573.      ;        INTB_RBF
  1574.      ;
  1575.      ;  a1 - custom chips base
  1576.      ;
  1577.      cnop 0,4 ; align for 020+
  1578. level5:
  1579.      movem.l  d0/a0,-(sp)
  1580.      move.l   #custom+serdatr,a0              ; get ptr to custom regs
  1581.      ;
  1582.      move.l   intenar-serdatr(a0),d0          ; get intenar & intreq
  1583.      btst      #INTB_INTEN+16,d0              ; interrupts enabled?
  1584.      beq.b      41$                      ; nope, ignore
  1585.      btst      #INTB_RBF,d0                  ; receive buffer full?
  1586.      beq.b      level5n                  ; nope, invoke old handler
  1587.      ;
  1588. 10$     move.w   (a0),d0                  ; Overrun?
  1589.      bpl.b      20$                      ; nope, branch
  1590.      ;
  1591.      ;      We've missed some data, so set overrun flag.
  1592.      ;
  1593.      addq.l   #1,Overrun                  ; set overrun flag
  1594. 20$     move.w   #INTF_RBF,intreq-serdatr(a0)          ; clear RBF interrupt
  1595.      move.l   i_BufIn(pc),a0              ; get current ptr
  1596.      move.b   d0,(a0)+                  ; store received byte
  1597.      addq.l   #1,i_InCnt                  ; incr bytes in buffer
  1598.      ;
  1599.      cmpa.l   i_BufEnd(pc),a0              ; hit end of buffer?
  1600.      bne.b      30$                      ; nope, branch
  1601.      ;
  1602.      ;      Hit physical end of buffer, so wrap to the start of the buffer.
  1603.      ;
  1604.      movea.l  i_BufPtr(pc),a0              ; get buffer ptr
  1605. 30$     move.l   a0,i_BufIn                  ; store input ptr
  1606.      ;
  1607.      subq.l   #1,i_Thresh                  ; close to full buffer?
  1608.      bne.b      40$                      ; nope, branch
  1609.      ;
  1610.      ;      Hit buffer threshold, so tell other end not to send any more
  1611.      ;      data.
  1612.      ;
  1613.      tst.l      Handshake(pc)               ; are we handshaking?
  1614.      beq.b      40$                      ; nope, skip RTS
  1615.      ori.b      #1<<CIAB_COMRTS,_ciabpra          ; block further input
  1616.      ;
  1617. 40$
  1618.     IFEQ NEWCODE
  1619.      move.l   #custom+serdatr,a0              ; get ptr to custom regs
  1620.      btst      #INTB_RBF&7,intreqr-serdatr(a0)     ; receive buffer full?
  1621.      bne.b      10$                      ; yep, go get another byte
  1622.     ENDC
  1623.      ;
  1624. 41$     movem.l  (sp)+,d0/a0
  1625.      rte                          ; return
  1626.      ;
  1627.      ;
  1628.      ;  a1 - IS_DATA
  1629.      ;  a5 - jump vector register
  1630.      ;
  1631.      cnop 0,4 ; align for 020+
  1632. level2:
  1633.      ;
  1634.      ;      If there's nothing in the buffer, there's no point in going
  1635.      ;      any further.
  1636.      ;
  1637.      tst.l      i_InCnt(pc)                  ; anything in the buffer?
  1638.      beq.b      9$                      ; nope, branch
  1639.      ;
  1640.      ;      If we've been "disabled" then get out.
  1641.      ;
  1642. 20$     tst.l      disableRead(pc)              ; internally disabled?
  1643.      bge.b      10$                      ; yep, get out of here
  1644.      lea      Start(pc),a5                  ; get base
  1645.      ;
  1646.      ;      If we have an active request, branch down and try to fulfill it.
  1647.      ;
  1648.      move.l   cr_IOReq(pc),d0              ; get and test active I/O
  1649.      bne.b      30$                      ; nzero, active, branch
  1650.      ;
  1651.      ;      Get first node in list and test if empty.
  1652.      ;
  1653.      move.l   (a1),a0                  ; get head of list
  1654.      move.l   (a0),d0                  ; get successor
  1655.      beq.b      9$                      ; end of list? yep, branch
  1656.      ;
  1657.      ;      Remove the node from the list
  1658.      ;
  1659.      move.l   d0,(a1)                  ; make new head
  1660.      exg.l      d0,a0                   ; swap nodes
  1661.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1662.      ;
  1663.      ;      Setup fields for processing a read request
  1664.      ;
  1665.      movea.l  d0,a1                   ; get I/O request
  1666.      move.b   IO_FLAGS(a1),d1
  1667.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1668.      move.l   a1,cr_IOReq-Start(a5)           ; store I/O request
  1669.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1670.      move.l   IO_DATA(a1),cr_OutPtr-Start(a5)     ; get/set output ptr
  1671.      move.b   d1,IO_FLAGS(a1)
  1672.      move.l   IO_LENGTH(a1),cr_Length-Start(a5)   ; get/set output count
  1673.      ;
  1674.      ;      Process an active I/O request
  1675.      ;
  1676. 30$     movea.l  SysBase(pc),a6              ; get ExecBase
  1677.      move.l   d0,a1                   ; get I/O request
  1678.      bsr.b      copyData                  ; go copy 'em
  1679.      tst.l      d0                      ; done with request?
  1680.      beq.b      9$                      ; nope, branch
  1681.      ;
  1682.      ;      the request has been satisfied, so return it.
  1683.      ;
  1684.      clr.l      cr_IOReq-Start(a5)              ; clear request
  1685.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1686.      jsr      _LVOReplyMsg(a6)              ; return I/O
  1687. 10$     moveq      #0,d0                   ; set Z flag
  1688. 9$     rts                          ; return
  1689.      ;
  1690.      ;      Registers:
  1691.      ;      Entry:   A1        Ptr to I/O Request
  1692.      ;           A6        ExecBase
  1693.      ;      Exit:    D0        ZERO - request not done, do not reply it
  1694.      ;                NZERO - request done, reply it
  1695.      ;
  1696.      cnop 0,4 ; align for 020+
  1697. copyData:
  1698.      ;
  1699.      ;      Process an active I/O request (or fall through from above)
  1700.      ;
  1701.      movem.l  d2-d5/a1-a5,-(sp)              ; save registers
  1702.      lea      Start(pc),a5                  ; get base
  1703.      movea.l  a1,a4                   ; get I/O request
  1704.      ;
  1705.      movea.l  i_BufOut(pc),a2              ; get current bufout ptr
  1706.      movea.l  cr_OutPtr(pc),a3              ; get current output ptr
  1707.      move.l   cr_Length(pc),d2              ; get current bytes needed
  1708.      move.l   i_InCnt(pc),d3              ; get current bytes in buffer
  1709.      ;
  1710.      ;      If we don't have enough bytes to satisfy the request,
  1711.      ;      set the length to the number of bytes we do have.
  1712.      ;
  1713.      cmp.l      d2,d3                   ; enuf to satisfy request?
  1714.      bge.b      15$                      ; yep, so branch
  1715.      move.l   d3,d2                   ; # to copy = # in buffer
  1716.      bra.b      16$                      ; branch to loop entry
  1717.      ;
  1718.      ;      Start of copy loop.
  1719.      ;
  1720.      cnop 0,4 ; align for 020+
  1721. 10$     movea.l  i_BufPtr(pc),a2              ; reset bufout to start
  1722.      ;
  1723.      ;      Entry point of copy loop.
  1724.      ;
  1725. 15$     move.l   d2,d3                   ; xfer # of bytes to copy
  1726. 16$
  1727.      ;
  1728.      ;      If the copy will extend past the end of the buffer, we can
  1729.      ;      only copy the number of bytes to the end this go around.
  1730.      ;
  1731.      move.l   i_BufEnd(pc),d0              ; get ptr to end of buf
  1732.      sub.l      a2,d0                   ; calc # of bytes to end
  1733.      cmp.l      d0,d3                   ; # to copy < # to end?
  1734.      blt.b      20$                      ; yep, branch
  1735.      move.l   d0,d3                   ; get bytes to end
  1736.      ;
  1737.      ;      Registers:
  1738.      ;
  1739.      ;      A2 = pointer from which data will be copied
  1740.      ;      A3 = pointer to which data will be copied
  1741.      ;      D2 = number of bytes that need to be copied
  1742.      ;      D3 = number of bytes to copy this iteration
  1743.      ;
  1744. 20$     move.l   d3,d0                   ; get length
  1745.     IFNE EOFCODE
  1746.      btst      #SERB_EOFMODE,IO_SERFLAGS(a4)       ; EOFMODE requested?
  1747.      beq.b      30$                      ; nope, just go copy
  1748.      ;
  1749.      ;      EOFMODE was specified so copy characters 1 at a time until
  1750.      ;      we hit an EOF character, the output butter has filled, or
  1751.      ;      the input buffer has drained.
  1752.      ;
  1753.      movem.l  IO_TERMARRAY(a4),d4/d5          ; get termarry
  1754.      rol.l      #8,d4
  1755.      move.l   d4,a1
  1756.      rol.l      #8,d5
  1757.      ;
  1758. 21$     move.b   (a2)+,d1                  ; get byte
  1759.      ; This way the term char IS copied (if term char is not copied eofmode will fail)
  1760.      move.b   d1,(a3)+                  ; put in output buffer
  1761.      ;
  1762.      cmp.b      d4,d1                   ; found term char?
  1763.      bge.b      22$                      ; possibly, branch (taken 0, not taken 1)
  1764.      rol.l      #8,d4
  1765.      cmp.b      d4,d1                   ; found term char?
  1766.      bge.b      22$                      ; possibly, branch
  1767.      rol.l      #8,d4
  1768.      cmp.b      d4,d1                   ; found term char?
  1769.      bge.b      22$                      ; possibly, branch
  1770.      rol.l      #8,d4
  1771.      cmp.b      d4,d1                   ; found term char?
  1772.      bge.b      22$                      ; possibly, branch
  1773.      move.l   d5,d4
  1774.      cmp.b      d4,d1                   ; found term char?
  1775.      bge.b      22$                      ; possibly, branch
  1776.      rol.l      #8,d4
  1777.      cmp.b      d4,d1                   ; found term char?
  1778.      bge.b      22$                      ; possibly, branch
  1779.      rol.l      #8,d4
  1780.      cmp.b      d4,d1                   ; found term char?
  1781.      bge.b      22$                      ; possibly, branch
  1782.      rol.l      #8,d4
  1783.      cmp.b      d4,d1                   ; found term char?
  1784. 22$     beq.b      24$                      ; term char found?
  1785.      ;
  1786.      ;      Didn't find a term character, so continue with the copy loop
  1787.      ;
  1788. 23$     move.l   a1,d4
  1789.      subq.l   #1,d0                   ; decr length counter
  1790.      bne.b      21$                      ; continue if more
  1791.      bra.b      40$                      ; done with copy, branch
  1792.      ;
  1793.      ;      We've found a termination character.
  1794.      ;
  1795.      cnop 0,4 ; align for 020+
  1796. 24$     clr.l      cr_Length-Start(a5)              ; done with request
  1797.      bra.b      50$                      ; branch
  1798.      cnop 0,4 ; align for 020+
  1799.     ENDC
  1800.      ;
  1801.      ;      EOFMODE not specified, so just do a bulk copy.
  1802.      ;
  1803.      ;      XXX POSSIBLE SPEEDUP XXX
  1804.      ;
  1805.      ;      For short copies it would be quicker to have a simple inline
  1806.      ;      loop, but what's short???  It would be different by CPU.
  1807.      ;
  1808. 30$     movea.l  a3,a1                   ; where to put it
  1809.      movea.l  a2,a0                   ; where to get it
  1810. ; NB: buffer size is multiple of 64bytes (not contents size)
  1811. ; most copys are 1 or 2 bytes, max copy usually seen is 256 bytes
  1812.      jsr     _LVOCopyMem(a6)    ; we use copymemquicker patch by *Art
  1813.      adda.l   d3,a2                   ; update bufout
  1814.      adda.l   d3,a3                   ; update outptr
  1815.      ;
  1816.      ;      Fall through and entered from EOFMODE loop
  1817.      ;
  1818.      ;      If the following calculation results in a value greater than
  1819.      ;      zero, then we have a buffer wrap and need to process the
  1820.      ;      remaining bytes at the beginning of the buffer.
  1821.      ;
  1822. 40$     sub.l      d3,d2                   ; calc bytes left to copy
  1823.      bgt.b      10$                      ; >0, more to copy, branch
  1824.      ;
  1825.      ;
  1826.      ;
  1827. 50$     move.l   a3,d1                   ; get outptr
  1828.      sub.l      cr_OutPtr(pc),d1              ; calc length
  1829.      move.l   a3,cr_OutPtr-Start(a5)          ; update outptr
  1830.      ;
  1831.      ;      Update output ptr and I/O request
  1832.      ;
  1833.      move.l   a2,i_BufOut-Start(a5)           ; store bufout ptr
  1834.      add.l      d1,IO_ACTUAL(a4)              ; update I/O request
  1835.      ;
  1836.      ;      Update number of bytes left in the buffer.
  1837.      ;
  1838.      sub.l      d1,i_InCnt-Start(a5)              ; calc bytes left in buffer
  1839.      ;
  1840.      ;      If the threshold becomes positive here, then, if requested,
  1841.      ;      tell the other end that it's okay to start sending more data.
  1842.      ;
  1843.      add.l      d1,i_Thresh-Start(a5)           ; calc thresh and test
  1844.      ble.b      60$                      ; <= 0, need more data, skip
  1845.      tst.l      Handshake(pc)               ; are we handshaking?
  1846.      beq.b      60$                      ; nope, skip RTS
  1847.      andi.b   #~(1<<CIAB_COMRTS),_ciabpra          ; ready to recieve more data
  1848.      ;
  1849.      ;      Set error if we've had any overruns.
  1850.      ;
  1851. 60$     tst.l      Overrun(pc)                  ; did overrun occur?
  1852.      beq.b      70$                      ; nope, branch
  1853.      clr.l      Overrun-Start(a5)              ; reset overrun flag
  1854.      move.b   #SerErr_LineErr,IO_ERROR(a4)          ; set error code
  1855.      ;
  1856.      ;      Calc number of bytes left to copy.  If the result is greater
  1857.      ;      than zero, then we have more to copy so return an incomplete
  1858.      ;      status.
  1859.      ;
  1860. 70$     moveq      #0,d0                   ; assume I/O incomplete
  1861.      sub.l      d1,cr_Length-Start(a5)          ; update length and test
  1862.      bgt.b      90$                      ; >0, more to do, branch
  1863.      ;
  1864.      ;      We've completed the I/O request either by copying the requested
  1865.      ;      of bytes or by finding an EOFMODE character, so return a "reply"
  1866.      ;      status.
  1867.      ;
  1868.      moveq      #1,d0                   ; indicate reply
  1869.      ;
  1870.      ;      Restore and exit
  1871.      ;
  1872. 90$
  1873.      movem.l  (sp)+,d2-d5/a1-a5
  1874.      rts                          ; return (status in D0)
  1875.      ;
  1876.      ;      Align data
  1877.      ;
  1878.      CNOP      0,4
  1879.      ;
  1880.      ;
  1881.      ;
  1882. Init:
  1883.      DC.L      sizeof_Base8n1
  1884.      DC.L      funcTab
  1885.      DC.L      dataTab
  1886.      DC.L      InitRoutine
  1887.      ;
  1888.      ;
  1889.      ;
  1890. funcTab:
  1891.      DC.W      -1
  1892.      DC.W      dev_Open-funcTab
  1893.      DC.W      dev_Close-funcTab
  1894.      DC.W      dev_Expunge-funcTab
  1895.      DC.W      dev_Null-funcTab
  1896.      DC.W      dev_BeginIO-funcTab
  1897.      DC.W      dev_AbortIO-funcTab
  1898.      DC.W      -1
  1899.      ;
  1900.      ;
  1901.      ;
  1902. dataTab:
  1903.      INITBYTE LN_TYPE,NT_DEVICE
  1904.      INITLONG LN_NAME,Name
  1905.      INITBYTE LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
  1906.      INITWORD LIB_VERSION,VERSION
  1907.      INITWORD LIB_REVISION,REVISION
  1908.      INITLONG LIB_IDSTRING,IdString
  1909.      DC.W      0
  1910.      ;
  1911.      ;      String Constants
  1912.      ;
  1913. miscresource:
  1914.      DC.B      "misc.resource",0
  1915. timerdevice:
  1916.      DC.B      "timer.device",0
  1917. intuitlib:
  1918.      DC.B      "intuition.library",0
  1919. Name:
  1920.      DC.B      "8n1.device",0
  1921. IdString:
  1922.      VSTRING
  1923.      ;
  1924.      ;      End of checksummed area.  (Realigns data too!)
  1925.      ;
  1926. ENDTag:
  1927.      CNOP      0,4
  1928.      ;
  1929.      ;      Global SysBase (Use instead of _AbsExecBase for speed)
  1930.      ;
  1931. SysBase:
  1932.      DC.L      0
  1933.      ;
  1934.      ;      Internal buffer tracking (DO NOT CHANGE THE ORDER!!!!)
  1935.      ;
  1936. i_BufPtr:
  1937.      DC.L      0
  1938. i_BufIn:
  1939.      DC.L      0
  1940. i_BufOut:
  1941.      DC.L      0
  1942. i_BufEnd:
  1943.      DC.L      0
  1944. i_InCnt:
  1945.      DC.L      0
  1946. i_Thresh:
  1947.      DC.L      0
  1948.      ;
  1949.      ;      Used while processing a read request.
  1950.      ;
  1951. cr_IOReq:
  1952.      DC.L      0
  1953. cr_OutPtr:
  1954.      DC.L      0
  1955. cr_Length:
  1956.      DC.L      0
  1957.      ;
  1958.      ;      List head for read requests
  1959.      ;
  1960. readQ:
  1961.      DC.L      readQ+MLH_TAIL
  1962.      DC.L      0
  1963.      DC.L      readQ
  1964.      ;
  1965.      ;      Write control.
  1966.      ;
  1967. cw_Length:
  1968.      DC.L      0
  1969. cw_Buffer:
  1970.      DC.L      0
  1971. cw_IOReq:
  1972.      DC.L      0
  1973.      ;
  1974.      ;      List head for write requests
  1975.      ;
  1976. writeQ:
  1977.      DC.L      writeQ+MLH_TAIL
  1978.      DC.L      0
  1979.      DC.L      writeQ
  1980.      ;
  1981.      ;
  1982.      ;
  1983. timerPort:
  1984.      DC.L      0                      ; LN_SUCC
  1985.      DC.L      0                      ; LN_PRED
  1986.      DC.B      NT_MSGPORT                  ; LN_TYPE
  1987.      DC.B      0                      ; LN_PRI
  1988.      DC.L      0                      ; LN_NAME
  1989.      DC.B      3                      ; MP_FLAGS (undoc'ed)
  1990.      DC.B      0                      ; MP_SIGBIT
  1991.      DC.L      timerRtn                  ; MP_SIGTASK
  1992.      DC.L      timerPort+MP_MSGLIST+LH_TAIL          ; LH_HEAD
  1993.      DC.L      0                      ; LH_TAIL
  1994.      DC.L      timerPort+MP_MSGLIST              ; LH_TAILPRED
  1995.      DC.B      0                      ; LH_TYPE
  1996.      DC.B      0                      ; LH_pad
  1997.      DC.W      0                      ; long align
  1998.      ;
  1999.      ;
  2000.      ;
  2001. timerReq:
  2002.      DC.L      0                      ; LN_SUCC
  2003.      DC.L      0                      ; LN_PRED
  2004.      DC.B      NT_MESSAGE                  ; LN_TYPE
  2005.      DC.B      0                      ; LN_PRI
  2006.      DC.L      0                      ; LN_NAME
  2007.      DC.L      timerPort                  ; MN_REPLYPORT
  2008.      DC.W      IOTV_SIZE                  ; MN_LENGTH
  2009.      DC.L      0                      ; IO_DEVICE
  2010.      DC.L      0                      ; IO_UNIT
  2011.      DC.W      TR_ADDREQUEST               ; IO_COMMAND
  2012.      DC.B      0                      ; IO_FLAGS
  2013.      DC.B      0                      ; IO_ERROR
  2014.      DC.L      0                      ; TV_SECS
  2015.      DC.L      0                      ; TV_MICROS
  2016.      ;
  2017.      ;
  2018.      ;
  2019. VBInterrupt:
  2020.      DC.L      0                      ; LN_SUCC
  2021.      DC.L      0                      ; LN_PRED
  2022.      DC.B      NT_INTERRUPT                  ; LN_TYPE
  2023.      DC.B      0                      ; LN_PRI
  2024.      DC.L      Name                      ; LN_NAME
  2025.      DC.L      readQ                   ; IS_DATA
  2026.      DC.L      level2                  ; IS_CODE
  2027.      ;
  2028.      ;      Global flags
  2029.      ;
  2030.      CNOP      0,4
  2031. Overrun:
  2032.      DC.L      0
  2033. Handshake:
  2034.      DC.L      1
  2035. disableRead:
  2036.      DC.L      -1
  2037. cmdlist:
  2038.      dc.w      CMD_RESET
  2039.      dc.w      CMD_READ
  2040.      dc.w      CMD_WRITE
  2041.      dc.w      CMD_CLEAR
  2042.      dc.w      CMD_FLUSH
  2043.      dc.w      SDCMD_QUERY
  2044.      dc.w      SDCMD_BREAK
  2045.      dc.w      SDCMD_SETPARAMS
  2046.      dc.w      NSCMD_DEVICEQUERY
  2047.      dc.w      0
  2048.      ;
  2049.      ;
  2050.      ;
  2051.      END
  2052.  
  2053.